package com.hxd.otc.module.home.presenter;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseRecyclerPresenter;
import com.hxd.otc.module.home.adapter.TicketListAdapter;
import com.hxd.otc.module.home.contract.TicketListContract;
import com.hxd.otc.utils.GsonUtil;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.ValueCallBack;
import com.hyphenate.helpdesk.domain.TicketEntity;
import com.hyphenate.helpdesk.domain.TicketListResponse;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * Created by lzy on 2019/1/15 0015.
 * description ${留言列表}
 */
public class TicketListPresenter extends BaseRecyclerPresenter<TicketListContract.IView, TicketListAdapter, TicketEntity> implements TicketListContract.IPresenter {

    private static final int MSG_LOAD_MORE_DATA = 0x01;
    private static final int MSG_REFRESH_DATA = 0x02;
    private static final int MSG_LOAD_ERROR = 0x03;

    private WeakHandler mWeakHandler;

    public TicketListPresenter(TicketListContract.IView iView) {
        super(iView);
        mWeakHandler = new WeakHandler(this);
    }

    @Override
    public void loadData(final boolean isRefresh) {
        String target = Constant.HyphenateKeFu.DEFAULT_CUSTOMER_ACCOUNT;
        String projectId = Constant.HyphenateKeFu.DEFAULT_PROJECT_ID;
        if (TextUtils.isEmpty(target) || TextUtils.isEmpty(projectId)) {
            return;
        }

        ChatClient.getInstance().leaveMsgManager().getLeaveMsgs(projectId, target, mPageIndex, getPageSize(), new ValueCallBack<String>() {
            @Override
            public void onSuccess(String value) {
                Message message = mWeakHandler.obtainMessage();
                TicketListResponse ticketListResponse = GsonUtil.fromJson(value, TicketListResponse.class);
                message.what = MSG_REFRESH_DATA;
                message.obj = ticketListResponse.getEntities();
                mWeakHandler.sendMessage(message);
            }

            @Override
            public void onError(int error, String errorMsg) {
                Message message = mWeakHandler.obtainMessage();
                message.what = MSG_LOAD_ERROR;
                message.obj = isRefresh;
                mWeakHandler.sendMessage(message);
            }
        });
    }

    private static class WeakHandler extends Handler {
        WeakReference<TicketListPresenter> weakReference;

        public WeakHandler(TicketListPresenter presenter) {
            this.weakReference = new WeakReference<TicketListPresenter>(presenter);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TicketListPresenter presenter = weakReference.get();
            if (presenter != null) {
                switch (msg.what) {
                    case MSG_LOAD_MORE_DATA:
                        presenter.onLoadDataSuccess(false, (List<TicketEntity>) msg.obj);
                        break;
                    case MSG_REFRESH_DATA:
                        presenter.onLoadDataSuccess(true, (List<TicketEntity>) msg.obj);
                        break;
                    case MSG_LOAD_ERROR:
                        presenter.onLoadDataFail((Boolean) msg.obj, "");
                        break;
                    default:
                        break;
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        mPageIndex = 0;
        mView.setEnableLoadMore(mView.isEnableLoadMore());
        loadData(true);
    }
}
